package com.hb.game.pk.ui.activity.mine.info;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.Login;
import com.hb.game.pk.event.ChangeHeadEvent;
import com.hb.game.pk.event.RefreshUserInfoEvent;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.widgets.MineItemView;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;


public class InfoActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.iv_image)
    ImageView iv_right;
    @BindView(R.id.miv_nickname)
    MineItemView mivNickname;
    @BindView(R.id.miv_account)
    MineItemView mivAccount;
    @BindView(R.id.rl_er_code)
    RelativeLayout rlErCode;
    @BindView(R.id.miv_phone)
    MineItemView mivPhone;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_info;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InfoActivity.this.finish();
            }
        });
        Login.DataBean userInfo = APP.userInfo;
        String avatar = userInfo.getFace();
        String trim = StringUtils.getString(avatar);
        int res = 0;
        if (StringUtils.isEmpty(trim)) {
            res = R.mipmap.ic_head_normal;
        } else {
            int i = Integer.parseInt(trim);
            res = APP.headResource.get(i - 1);
        }
        iv_right.setBackgroundResource(res);
        mivNickname.setRightText(userInfo.getNickname());
        mivAccount.setRightText(userInfo.getAccount());
        mivPhone.setRightText(userInfo.getMobile());
    }

    @OnClick({R.id.rl_head, R.id.miv_nickname, R.id.miv_account, R.id.rl_er_code, R.id.miv_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_head:
                startActivity(ChooseHeadActivity.class);
                break;
            case R.id.miv_nickname:
                startActivity(ChangeNameActivity.class);
                break;
            case R.id.miv_account:
                break;
            case R.id.rl_er_code:
//                if (erDialog == null) {
//                    View inflate = View.inflate(this, R.layout.dialog_er, null);
//                    ImageView viewById = (ImageView) inflate.findViewById(R.id.iv_er);
//                    erDialog = new MaterialDialog.Builder(this)
//                            .customView(inflate, false).show();
//                } else {
//                    if (erDialog.isShowing()) {
//                        erDialog.dismiss();
//                    } else {
//                        erDialog.show();
//                    }
//                }
                break;
            case R.id.miv_phone:
                break;
        }
    }

    @Subscribe
    public void onEvent(ChangeHeadEvent e) {
        iv_right.setBackgroundResource(ChangeHeadEvent.res);
    }

    @Subscribe
    public void onEvent(RefreshUserInfoEvent e) {
        mivNickname.setRightText(RefreshUserInfoEvent.name);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
